﻿using DL.Character.Core.Base.Movement;
using DL.Characters.Player.FSM.StateMachine;
using Tools.Extensions;
using UnityEngine;

namespace DL.Characters.Player.FSM.State
{
    /// <summary>
    /// 
    /// </summary>
    public class PlayerDodgeState : PlayerMovementState
    {
        private CustomMovementSet[] m_Data;

        public PlayerDodgeState(PlayerMoveStateMachine playerStateMachine, int property = 0) : base(playerStateMachine,
            property)
        {
            // this.priority = 1; //高优先级
            m_Data = StateMachine.Player.CharacterSO.MovementActionConfigSO.DodgeCustomMovementSet;
        }

        private int m_Index;

        public override void Enter()
        {
            base.Enter();
            ReusableData.InputMult = 0f;
            ReusableData.IsDodge = true;
            StateMachine.MovementController.CalculateTurnAngles(ReusableData.InputDirection);
            string aniName;
            //判断方向
            //左边 -180，0 右边 0，180
            if (!ReusableData.TargetAngle.Approx(0f))
            {
                //右边
                if (ReusableData.TargetAngle > 45f && ReusableData.TargetAngle < 135f)
                {
                    aniName = "Dodge_R";
                    m_Index = 3;
                }
                //左边
                else if (ReusableData.TargetAngle < -45f && ReusableData.TargetAngle > -135f)
                {
                    aniName = "Dodge_L";
                    m_Index = 2;
                }
                //向前 
                else
                {
                    aniName = "Dodge_F";
                    m_Index = 0;
                }
            }
            //没有按下，向后
            else
            {
                aniName = "Dodge_B";
                m_Index = 1;
            }
            Debug.Log("播放动画 " + aniName);
            Animator.Play(aniName, -1, .25f);
            StateMachine.Player.CustomMovementController.CustomMovementMethod(m_Data[m_Index]);
        }

        public override void Update()
        {
            StateExitEvent();
        }

        public override void FixedUpdate()
        {
            StateMachine.MovementController.SimulateChracterGravity();
            StateMachine.MovementController.CharacterVerticalVelocityInterface();
        }

        protected override void AddInputActionCallBacks()
        {
            StateMachine.Player.AnimationEvent.DodgeCalcelEvent += DodgeCalcel;
        }

        protected override void RemoveInputActionCallBacks()
        {
            StateMachine.Player.AnimationEvent.DodgeCalcelEvent -= DodgeCalcel;
        }

        // public override void OnAnimationExitEvent()
        // {
        //     StateExitEvent();
        // }

        private void StateExitEvent()
        {
            if (CanSprint())
            {
                StateMachine.ChangeState<PlayerSprintState>();
            }
            else if (CanLocomotion())
            {
                StateMachine.ChangeState<PlayerLocomotionState>();
            }
        }

        private void DodgeCalcel()
        {
            ReusableData.IsDodge = false;
        }

        //********************转换条件检查*************************
        private bool CanLocomotion()
        {
            if (!ReusableData.IsDodge && ReusableData.InputDirection == Vector2.zero &&
                MoveController.CharacterIsOnGround && !StateMachine.AnimatorIsInTransition)
            {
                return true;
            }
            return false;
        }

        private bool CanSprint()
        {
            if (!ReusableData.IsDodge && ReusableData.InputDirection != Vector2.zero &&
                MoveController.CharacterIsOnGround && !StateMachine.AnimatorIsInTransition)
            {
                return true;
            }
            return false;
        }

    }
}